
// store 仓库 
import Vue  from "vue"
import Vuex from "vuex"

Vue.use(Vuex)

import {city} from "./city"

// vuex 所有的state action mutation 放在 store 
const store = new Vuex.Store({

    modules:{   // 把 store 进行拆分
        city
    },

    // 组件的状态 (data)  vuex 所有的数据放到一个大对象(State )
    state:{
        count:2004,
        word:"daydayup",
        banner:null,
        data:{
            tiwenList:null,
        },
        active:"0",
        msg:"I am fine"
    },
    actions:{
        // 异步数据请求使用 action  
        changeMsgAction({commit},msg){  //context = {commit:commit(){}}  = {commit} 
            commit("changeMsg",msg);  // action 一定提交mutation  通知 mutations 去修改数据 
        }
    },
    mutations:{
        changeMsg(state,payload){
            state.msg = payload;
        },
        // mutation 同步 
        changeCount(state){   // payload 载荷  参数
            state.count++;
        },
        changeCountByNum(state,payload){
            // state.count = payload;
            // 每次返回一个新的对象
            // state = {...state,count:state.count+payload};
            state.count += payload
        },
        changeWord(state,payload){
            state.word = payload;
        },
        changeBanner(state,payload){
            state.banner = payload;
        },
        changeTiwenList(state,payload){
            state.data = {...state.data,tiwenList:payload}
        },
        changeActive(state,payload){
            state.active = payload;
        }
    },
    getters:{  // 依赖  参考computed get   
        // 做一下数据过滤筛选  优化
        newCount(state){
            return state.count * 10 + 888;
        }
    }
})

export default store;

